package demo1;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 98152
 * Date: 2024-07-09
 * Time: 11:54
 */
public class MyLinkedList implements IList{
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode tail;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = tail = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = tail = node;
            return;
        }
        node.prev = tail;
        tail.next = node;
        tail = node;
    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        if(index < 0 || index > len) {
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == len) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        node.prev = cur;
        node.next = cur.next;
        cur.next.prev = node;
        cur.next = node;
    }

    private ListNode findIndex(int index) {
        ListNode cur = head;
        for(int i = 0; i < index-1; i++) {
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }
        ListNode del = findKeyOfIndex(key);
        if(del == null) {
            return;
        }
        if(del.prev == null) {
            head = head.next;
            if(head != null) {
                head.prev = null;
            }
            return;
        }
        if(del.next == null) {
            tail = tail.prev;
            tail.next = null;
            return;
        }
        del.prev.next = del.next;
        del.next.prev = del.prev;
    }
    private ListNode findKeyOfIndex(int key) {
        ListNode cur = head;
        ListNode last = tail;
        while(cur != last) {
            if(cur.val == key) {
                return cur;
            }
            if(last.val == key) {
                return last;
            }
            cur = cur.next;
            last = last.prev;
        }
        if(cur.val == key) {
            return cur;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode start = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                start.next = cur.next;
                cur = cur.next;
                if(cur != null) {
                    cur.prev = start;
                }
            }else {
                start = cur;
                cur = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }
    }

    @Override
    public int size() {
        int len = 0;
        ListNode cur = head;
        while(cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode curN = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curN;
        }
        head = null;
        tail = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
